﻿package com.game.manager
{
	import com.game.engine.MainGame;
	import com.game.view.layer.GameMenu;
	import com.game.view.layer.gameMap.GameMapBase;
	import com.game.view.layer.gameMap.HellMap;
	import com.game.view.layer.gameMap.IslandMap;
	import com.game.view.layer.gameMap.NewWorldMap;
	import com.game.view.layer.gameMap.PalaceMap;
	import com.game.view.layer.gameMap.ParadiseMap;
	import com.greensock.TweenMax;
	import com.greensock.easing.Quad;
	
	import config.Config;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	
	public class GameSceneManager
	{
		
		private static var _instance:GameSceneManager;
		public static const GAME_MENU:String = "GameMenu";
		public static const GAME_MAP:String = "GameMap";
		public static const START_FIGHTING:String = "StartFighting";
		
		public static const MAP_PARADISE:String = "paradise";
		public static const MAP_HELL:String = "hell";
		public static const MAP_ISLAND:String = "island";
		public static const MAP_NEW_WORLD:String = "newWorld";
		public static const MAP_PALACE:String = "palace";
		
		public static function getIns():GameSceneManager
		{
			if (!_instance)
			{
				_instance = new GameSceneManager();
			}
			return _instance;
		}
		
		public function GameSceneManager():void
		{
			_instance = this;
		}
		
		public var mainStage:Stage;
		
		private var _mainLayer:Sprite;
		
		public function get mainLayer():Sprite
		{
			return this._mainLayer;
		}
		
		private var _topLayer:Sprite;
		
		public function get topLayer():Sprite
		{
			return this._topLayer;
		}
		
		public function init(gMain:GMain):void
		{
			mainStage = gMain.stage;
			_mainLayer = new Sprite();
			_topLayer = new Sprite();
			_mainLayer.x = _mainLayer.y = 0;
			_topLayer.x = _topLayer.y = 0;
			gMain.addChild(_mainLayer);
			gMain.addChild(_topLayer);
			var _mask:Shape = new Shape();
			with (_mask)
			{
				graphics.beginFill(0x000000);
				graphics.drawRect(0, 0, 940, 590);
				graphics.endFill();
				x = 0;
				y = 0;
			}
			gMain.stage.addChild(_mask);
			gMain.mask = _mask;
		}
		
		public function clearGameMap():void
		{
			var _loc1_:Sprite = this.mainLayer.getChildByName("currentShowedMap") as Sprite;
			if (_loc1_ != null)
			{
				this.mainLayer.removeChild(_loc1_);
			}
		}
		
		public function showGameMap(param1:String = MAP_PARADISE):void
		{
			this.clearGameMap();
			var map:GameMapBase;
			switch (param1)
			{
				case MAP_ISLAND:
					map = new IslandMap();
					SoundManager.playBgm("islandBgm");
					Config.getIns().userData.lastWorld = 0;
					break;
				case MAP_HELL:
					map = new HellMap();
					SoundManager.playBgm("hellBgm");
					Config.getIns().userData.lastWorld = 1;
					break;
				case MAP_PARADISE:
					map = new ParadiseMap();
					SoundManager.playBgm("paradiseBgm");
					Config.getIns().userData.lastWorld = 2;
					break;
				case MAP_NEW_WORLD:
					map = new NewWorldMap();
					SoundManager.playBgm("paradiseBgm");
					Config.getIns().userData.lastWorld = 3;
					break;
				case MAP_PALACE:
					map = new PalaceMap();
					SoundManager.playBgm("paradiseBgm");
					Config.getIns().userData.lastWorld = 4;
					break;
			}
			map.name = "currentShowedMap";
			map.alpha = 0;
			TweenMax.to(map, 0.4, {"alpha": 1, "ease": Quad.easeOut});
			this.addScene(map);
			NotificationManager.showLoginAlert();
		}
		
		public function switchScene(param1:String,stageStr:String = ""):void
		{
			switch (param1)
			{
				case START_FIGHTING:
					startFight(stageStr);
					break;
				case GAME_MENU:
					clearAllChild();
					showGameMenu();
					break;
				case GAME_MAP:
					showLastGameMap();
					break;
			}
		}
		
		public function addScene(child:DisplayObject, layer:String = "main"):void
		{
			if (layer == "top")
			{
				this.topLayer.addChild(child);
			}
			else if(layer == "main")
			{
				this.mainLayer.addChild(child);
			}
			else if(layer == "stage")
			{
				mainStage.addChild(child);
			}
		}
		
		public function contains(target:DisplayObject):Boolean
		{
			return this.mainLayer.contains(target) || this.topLayer.contains(target);
		}
		
		public function getChildByName(name:String):DisplayObject
		{
			var obj:DisplayObject;
			obj = _mainLayer.getChildByName(name);
			if(obj != null)
			{
				return obj;
			}
			obj = _topLayer.getChildByName(name);
			if(obj != null)
			{
				return obj;
			}
			obj = mainStage.getChildByName(name);
			if(obj != null)
			{
				return obj;
			}
			return null;
		}
		
		public function startFight(stageStr:String):void
		{
			clearGameMap();
			var arr:Array = stageStr.split("_");
			if(arr.length != 2)
			{
				return;
			}
			new MainGame(mainLayer,arr[0],arr[1]);
		}
		
		public function clearAllChild():void
		{
			var _loc1_:DisplayObject;
			while (this.mainLayer.numChildren > 0)
			{
				_loc1_ = this.mainLayer.getChildAt(0);
				this.mainLayer.removeChild(_loc1_);
			}
			while (this.topLayer.numChildren > 0)
			{
				_loc1_ = this.topLayer.getChildAt(0);
				this.topLayer.removeChild(_loc1_);
			}
		}
		
		private function showGameMenu():void
		{
			Config.getIns().initData();
			this.clearGameMap();
			var menu:GameMenu = new GameMenu();
			menu.name = "GameMenuHasShow";
			if (!mainLayer.getChildByName("GameMenuHasShow"))
			{
				mainLayer.addChild(menu);
			}
		}
		
		/**
		 * 显示上一个游戏地图
		 *
		 */
		private function showLastGameMap():void
		{
			var mapIdx:int = Config.getIns().userData.lastWorld;
			var maps:Vector.<String> = new <String>["island", "hell", "paradise", "newWorld", "palace"];
			try
			{
				var targetMap:String = maps[mapIdx];
			} catch (e:Error)
			{
				throw("目标地图序列没有对应的地图！");
			}
			this.showGameMap(targetMap);
		}
	}
}